﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Words;
using Aspose.Words.Tables;
using System.IO;
using Aspose.Words.Drawing;

namespace GeoWeb.Demo.Asposes
{
    /// <summary>
    /// Word文件生成导出示例
    /// add by zhangyu 
    /// 2013.01.16
    /// </summary>
    public partial class WordDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// word文件生成示例
        /// add by zhangyu
        /// 2013.01.16
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_WordReport_Click(object sender, EventArgs e)
         {
            //模版路径
            string templateFile = "word/word导出示例模版.doc";
            templateFile = Server.MapPath(templateFile);
            //LOGO图片路径
            string LogoFile = "word/Logo.gif";
            LogoFile = Server.MapPath(LogoFile);
            //导出word文件名
            string WordName = "word导出示例";

            try
            {
                Document doc = new Document(templateFile);
                DocumentBuilder builder = new DocumentBuilder(doc);
                //第一种根据标签插入值的方式
                Bookmark mark1 = doc.Range.Bookmarks["IsNeedMark"];
                mark1.Text = "是" + DateTime.Now.ToString("yyyy年MM月dd日");

                Bookmark mark2 = doc.Range.Bookmarks["IsWordMark"];
                mark2.Text = "是" + DateTime.Now.ToString("yyyy年MM月dd日");

                //Bookmark mark3 = doc.Range.Bookmarks["WhichDepart"];
                //mark3.Text = "工程二部" + DateTime.Now.ToString("yyyy年MM月dd日");
                //第二种方式
                builder.MoveToBookmark("WhichDepart");
                builder.InsertHtml("工程二部" + DateTime.Now.ToString("yyyy年MM月dd日"));
                //插入图片
                Shape shape = new Shape(doc, ShapeType.Image);
                shape.ImageData.SetImage(LogoFile);
                shape.Width = 593;
                shape.Height = 52;
                shape.HorizontalAlignment = HorizontalAlignment.Center; //靠右对齐

                builder.MoveToBookmark("LogoMark");
                builder.InsertNode(shape);

                System.IO.MemoryStream outStream = new MemoryStream();
                doc.Save(outStream, SaveFormat.Doc);

                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(WordName, System.Text.Encoding.UTF8).ToString() + ".doc");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                Response.BinaryWrite(outStream.ToArray());
            }
            catch (Exception ex)
            {
                Response.Clear();
                Response.Write(ex.ToString());
            }
            finally
            {
                Response.End();
            }
        }
    }
}